<?php
namespace app\index\controller\user;

use app\common\model\admin\AccountModel;
use app\common\model\paths\ClassModel;
use app\common\model\paths\UserClassModel;
use app\index\controller\BaseIndex;
use think\admin\model\SystemUser;

/**
 * 我的班级
 * @package app\index\controller\user
 * @author wangqinghui
 * @DateTime 2024-09-29 10:05:59
 */
class Classes extends BaseIndex
{
    // 是否强制登录
    protected $need_login = true;

    /**
     * 我的班级列表
     * @author wangqinghui
     * @DateTime 2024-09-29 15:18:26
     * @return void
     */
    public function index()
    {
        $this->title = lang('我的班级');
        $this->fetch('index');
    }

    /**
     * 获取课程列表
     * @author wangqinghui
     * @DateTime 2024-09-29 15:55:34
     * @return void
     */
    public function getList()
    {
        // 获取我的加入班级
        $map = [
            ['index_user_id',   '=',    $this->user_id],
            ['status',          '=',    1],
        ];
        // 分页
        $page = (int)$this->request->post('page', 1);
        $limit = (int)$this->request->post('limit', 10);
        $total = UserClassModel::mk()->where($map)->count();
        $list = UserClassModel::mk()->where($map)->page($page, $limit)->select()->toArray();

        // 获取班级
        $class_ids = array_column($list, 'class_id');
        $class_data = [];
        if(!empty($class_ids)){
            $class_data = ClassModel::mk()->field('id,name')->where('id', 'in', $class_ids)->column('name', 'id');
        }
        // 获取平台
        $platform_ids = array_column($list, 'platform_id');
        $platform_data = [];
        if(!empty($platform_ids)){
            $platform_data = AccountModel::mk()->where('id', 'in', $platform_ids)->column('title', 'id');
        }
        // 获取老师
        $teacher_ids = array_column($list, 'user_id');
        $teacher_data = [];
        if(!empty($teacher_ids)){
            $teacher_data = SystemUser::mk()->field('id,nickname')->where('id', 'in', $teacher_ids)->column('nickname', 'id');
        }
        if(!empty($list)){
            // 处理数据
            foreach($list as $k => $v){
                $list[$k]['class_name'] = !empty($class_data[$v['class_id']]) ? $class_data[$v['class_id']] : '--';
                $list[$k]['teacher_name'] = !empty($teacher_data[$v['user_id']]) ? $teacher_data[$v['user_id']] : '--';
                $list[$k]['platform_name'] = !empty($platform_data[$v['platform_id']]) ? $platform_data[$v['platform_id']] : '--';
            }
        }
        $this->success('获取成功', ['total' => $total, 'item' => $list], 0);
    }
}